/**
 * Copyright &copy; 2017 <a href="https://gitee.com/hackempire/emsite-parent">emsite</a> All rights reserved.
 */
package com.empire.emsite.modules.sys.utils;

import java.util.List;

import com.empire.emsite.common.service.BaseService;
import com.empire.emsite.common.utils.SpringContextHolder;
import com.empire.emsite.modules.sys.dao.AreaDao;
import com.empire.emsite.modules.sys.dao.OfficeDao;
import com.empire.emsite.modules.sys.dao.RoleDao;
import com.empire.emsite.modules.sys.dao.UserDao;
import com.empire.emsite.modules.sys.entity.Area;
import com.empire.emsite.modules.sys.entity.Office;
import com.empire.emsite.modules.sys.entity.Role;
import com.empire.emsite.modules.sys.entity.User;

//import com.empire.emsite.modules.sys.security.SystemAuthorizingRealm.Principal;
/**
 * 类UserDbsUtils.java的实现描述：用户工具类
 * 
 * @author arron 2017年10月30日 下午4:35:21
 */
public class UserDbsUtils {

    private static UserDao     userDao                       = SpringContextHolder.getBean(UserDao.class);
    private static RoleDao     roleDao                       = SpringContextHolder.getBean(RoleDao.class);
    //private static MenuDao     menuDao                       = SpringContextHolder.getBean(MenuDao.class);
    private static AreaDao     areaDao                       = SpringContextHolder.getBean(AreaDao.class);
    private static OfficeDao   officeDao                     = SpringContextHolder.getBean(OfficeDao.class);

    public static final String USER_CACHE                    = "userCache";
    public static final String USER_CACHE_ID_                = "id_";
    public static final String USER_CACHE_LOGIN_NAME_        = "ln";
    public static final String USER_CACHE_LIST_BY_OFFICE_ID_ = "oid_";

    public static final String CACHE_AUTH_INFO               = "authInfo";
    public static final String CACHE_ROLE_LIST               = "roleList";
    public static final String CACHE_MENU_LIST               = "menuList";
    public static final String CACHE_AREA_LIST               = "areaList";
    public static final String CACHE_OFFICE_LIST             = "officeList";
    public static final String CACHE_OFFICE_ALL_LIST         = "officeAllList";

    /**
     * 根据ID获取用户
     * 
     * @param id
     * @return 取不到返回null
     */
    public static User get(String id) {
        //User user = (User) EhCacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id);
        User user = null;
        if (user == null) {
            user = userDao.get(id);
            if (user == null) {
                return null;
            }
            user.setRoleList(roleDao.findList(new Role(user)));
            //EhCacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
            //EhCacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
        }
        return user;
    }

    /**
     * 根据登录名获取用户
     * 
     * @param loginName
     * @return 取不到返回null
     */
    public static User getByLoginName(String loginName) {
        //User user = (User) EhCacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName);
        User user = null;
        if (user == null) {
            user = userDao.getByLoginName(new User(null, loginName));
            if (user == null) {
                return null;
            }
            user.setRoleList(roleDao.findList(new Role(user)));
            //EhCacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user);
            //EhCacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
        }
        return user;
    }

    /**
     * 清除当前用户缓存
     */
    /*
     * public static void clearCache() { removeCache(CACHE_AUTH_INFO);
     * removeCache(CACHE_ROLE_LIST); removeCache(CACHE_MENU_LIST);
     * removeCache(CACHE_AREA_LIST); removeCache(CACHE_OFFICE_LIST);
     * removeCache(CACHE_OFFICE_ALL_LIST); UserUtils.clearCache(getUser()); }
     */

    /**
     * 清除指定用户缓存
     * 
     * @param user
     */
    /*
     * public static void clearCache(User user) {
     * EhCacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId());
     * EhCacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ +
     * user.getLoginName()); EhCacheUtils.remove(USER_CACHE,
     * USER_CACHE_LOGIN_NAME_ + user.getOldLoginName()); if (user.getOffice() !=
     * null && user.getOffice().getId() != null) {
     * EhCacheUtils.remove(USER_CACHE, USER_CACHE_LIST_BY_OFFICE_ID_ +
     * user.getOffice().getId()); } }
     */
    /**
     * 获取当前用户
     * 
     * @return 取不到返回 new User()
     */
    /*
     * public static User getUser() { Principal principal = null; if
     * (principal!=null){ User user = get(principal.getId()); if (user != null){
     * return user; } //return new User(); //} // 如果没有登录，则返回实例化空的User对象。 return
     * new User(); }
     */

    /**
     * 获取当前用户角色列表
     * 
     * @return
     */
    //    public static List<Role> getRoleList() {
    //        @SuppressWarnings("unchecked")
    //        List<Role> roleList = (List<Role>) getCache(CACHE_ROLE_LIST);
    //        if (roleList == null) {
    //            User user = getUser();
    //            if (user.isAdmin()) {
    //                roleList = roleDao.findAllList(new Role());
    //            } else {
    //                Role role = new Role();
    //                role.getSqlMap().put("dsf", BaseService.dataScopeFilter(user.getCurrentUser(), "o", "u"));
    //                roleList = roleDao.findList(role);
    //            }
    //            putCache(CACHE_ROLE_LIST, roleList);
    //        }
    //        return roleList;
    //    }

    /**
     * 获取当前用户授权菜单
     * 
     * @return
     */
    //    public static List<Menu> getMenuList() {
    //        @SuppressWarnings("unchecked")
    //        List<Menu> menuList = (List<Menu>) getCache(CACHE_MENU_LIST);
    //        if (menuList == null) {
    //            User user = getUser();
    //            if (user.isAdmin()) {
    //                menuList = menuDao.findAllList(new Menu());
    //            } else {
    //                Menu m = new Menu();
    //                m.setUserId(user.getId());
    //                menuList = menuDao.findByUserId(m);
    //            }
    //            putCache(CACHE_MENU_LIST, menuList);
    //        }
    //        return menuList;
    //    }

    /**
     * 获取当前用户授权的区域
     * 
     * @return
     */
    public static List<Area> getAreaList() {
        //@SuppressWarnings("unchecked")
        //List<Area> areaList = (List<Area>) getCache(CACHE_AREA_LIST);
        List<Area> areaList = null;
        if (areaList == null) {
            areaList = areaDao.findAllList(new Area());
            // putCache(CACHE_AREA_LIST, areaList);
        }
        return areaList;
    }

    /**
     * 获取当前用户有权限访问的部门
     * 
     * @return
     */
    public static List<Office> getOfficeList() {
        //@SuppressWarnings("unchecked")
        //List<Office> officeList = (List<Office>) getCache(CACHE_OFFICE_LIST);
        List<Office> officeList = null;
        if (officeList == null) {
            User user = new User();
            if (user.isAdmin()) {
                officeList = officeDao.findAllList(new Office());
            } else {
                Office office = new Office();
                office.getSqlMap().put("dsf", BaseService.dataScopeFilter(user, "a", ""));
                officeList = officeDao.findList(office);
            }
            //putCache(CACHE_OFFICE_LIST, officeList);
        }
        return officeList;
    }

    /**
     * 获取当前用户有权限访问的部门
     * 
     * @return
     */
    public static List<Office> getOfficeAllList() {
        //@SuppressWarnings("unchecked")
        //List<Office> officeList = (List<Office>) getCache(CACHE_OFFICE_ALL_LIST);
        List<Office> officeList = null;
        if (officeList == null) {
            officeList = officeDao.findAllList(new Office());
        }
        return officeList;
    }

    // ============== User Cache ==============

}
